<link rel="stylesheet" href="/static/layui/css/layui.css">
<link rel="stylesheet" href="/static/common.css?v=13">
<script src="/static/layui/layui.js"></script>
<script src="/static/common.js"></script>
<script src="/static/jquery-3.3.1.min.js"></script>
<body>
<ul class="layui-nav" lay-filter="" style="text-align: right;">

    {if condition="$isRoot"}
    <li class='layui-nav-item {eq name="url_route" value="/admin/user"}layui-this{/eq}'><a href="/admin/user">用户管理</a></li>
    {/if}
    {if condition="$isRoot"}
    <li class='layui-nav-item {eq name="url_route" value="/admin/api/group"}layui-this{/eq}'><a href="/admin/api/group">分组管理</a></li>
    {/if}
    <li class='layui-nav-item {eq name="url_route" value="/admin/api"}layui-this{/eq} '><a href="/admin/api">API管理</a></li>
    <li class='layui-nav-item'>
        <a href="javascript:void(0);" id="login_name">{$user_name ?? '未登录'}</a>
        <dl class="layui-nav-child">
            <dd><a href="javascript:void(0)" onclick="loginOut()">退出</a></dd>
        </dl>
    </li>
</ul>
<div id="userDiv" style="margin: 20px 50px 10px 0;display: none;">
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-block">
                <input type="text" name="name"  required  lay-verify="required" placeholder="请输入姓名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" style="margin-top: 20px">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="login">提交</button>
            </div>
        </div>
    </form>
</div>
<script>
    layui.use(['element','form','layer'], function(){
        var element = layui.element;
        var form = layui.form;
        var layer = layui.layer;
        $.ajax({
            type:'POST',
            url:'/admin/base/auth',
            data:{},
            success:function(data){
                if(!data){
                    layer.open({
                        type: 1,
                        title:'请登陆',
                        shade: [0.8, '#000'],
                        closeBtn: 0,
                        area: ['400px', '190px'],
                        content: $('#userDiv')
                    });
                }
                return false;
            }
        });

        form.on('submit(login)', function(data){
            let fields = data.field;
            $.ajax({
                type:'POST',
                url:'/admin/base/login',
                data:fields,
                success:function(data){
                    if(data.code === 0){
                        let result = data.data;
                        let name = fields.name;
                        if(true === result.is_root){
                            layer.prompt({
                                formType: 0,
                                value: '',
                                title: '请输入秘钥',
                            }, function(value, index, elem){
                                $.ajax({
                                    type:'POST',
                                    url:'/admin/base/login_root',
                                    data:{key:value},
                                    success:function(data){
                                        if(data.code === 0){
                                            layer.msg('登陆成功', {
                                                icon: 1,
                                                time:2000
                                            },function(){
                                                $("#login_name").html(name);
                                                element.render('nav');
                                            });
                                            layer.closeAll();
                                        }else{
                                            layer.msg(data.msg,{icon:5});
                                        }
                                        return false;
                                    }
                                });
                            });
                        }else{
                            layer.msg('登陆成功', {
                                icon: 1,
                                time:2000
                            },function(){
                                $("#login_name").html(name);
                                element.render('nav');
                            });
                            layer.closeAll();
                        }


                    }else{
                        layer.msg('登陆失败', {icon: 5});
                    }
                    return false;
                }
            });
            return false;
        });
    });

    function loginOut()
    {
        $.ajax({
        type:'POST',
        url:'/admin/base/login_out',
        data:{},
        success:function(data){
            layer.msg('退出成功', {
                icon: 1,
                time:1000
            },function(){
                location.reload();
            });
            return false;
        }
    });

    }
</script>
</body>